a=int(input())
mx=-float('inf')
c=a//10
c1=a//100
c2=a//1000
c3=a%10
c4=a%100
c5=a//10%100
c6=a

m=[x for x in range(0,a+a)]
for i in range(2,len(m)):
    for j in range(i+i,len(m),i):
        if m[i]!=0 and m[j]%m[i]==0:
            m[j]=0
m=sorted(list(set(m))[1:])
if  a<=1000:
    print(0)
elif int(str(c3*4))==a:
    print(0)
else:
    p=len(m)
    for n in range(1,p):
        if c6==m[n]:
           mx=max(c6,mx)
        else:
            pass
        if c==m[n]:
            mx=max(c,mx)
        else:
            pass
        if c1==m[n]:
            mx=max(c1,mx)
        else:
            pass
        if c2==m[n]:
            mx=max(c2,mx)
        else:
            pass
        if c3==m[n]:
            mx=max(c3,mx)
        else:
            pass
        if c4==m[n]:
            mx=max(c4,mx)
        else:
            pass
        if c5==m[n]:
            mx=max(c5,mx)
        else:
            pass
    if mx<0:
        print(0)
    else:
        print(mx)
